<?php

namespace app\admin\controller;

use app\admin\controller\Backend;
use \think\Db;

/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        $seventtime = \fast\Date::unixtime('day', -7);
        $paylist = $createlist = [];
        for ($i = 0; $i < 7; $i++)
        {
            $day = date("Y-m-d", $seventtime + ($i * 86400));

            $r = \think\Db::name('product_order')
                ->field("stage,COUNT(*) AS count")
                ->where(['' => ["exp", "DATE_FORMAT(FROM_UNIXTIME(create_time),'%Y-%m-%d') = date_format(now(),'".$day."')"]])
//                ->fetchsql(true)
                ->group('stage')
                ->select();

            $r = \fast\ArrayHelper::map($r, 'stage', 'count');

            $createlist[$day] = $r[0] ? : 0;
            $paylist[$day] = $r[99] ? : 0;
        }

        $totaluser = Db::name('user')->count();
        // 总分销商数
        $totaluserdistributor = Db::name('user')->where([
            'role' => 1,
        ])->count();
        // 总客户经理数
        $totalusercustomer = Db::name('user')->where([
            'role' => 2,
        ])->count();
        $totalorder = Db::name('product_order')->count();
        // 今日注册
        $todayusersignup = Db::name('user')->where([
            'create_time' => ['between', \think\helper\Time::today()]
        ])->count();
        // 今日登录
        $todayuserlogin = Db::name('user')->where([
            'update_time' => ['between', \think\helper\Time::today()]
        ])->count();
        // 今日贷款订单
        $todayorder = Db::name('product_order')->where([
            'create_time' => ['between', \think\helper\Time::today()]
        ])->count();
        // 进行中订单
        $todayunsettleorder = Db::name('product_order')->where([
            'status' => 0,
        ])->count();

        // 抢单中订单
        $product_order_reserve = Db::name('product_order')->where([
            'status' => 0,
            'stage' => 0,
            'customer_id' => 0,
        ])->count();

        // 审核中订单
        $product_order_review = Db::name('product_order')->where([
            'status' => 0,
            'stage' => 6,
        ])->count();



        $this->view->assign([
            'totaluser'          => $totaluser,
            'totaluserdistributor' => $totaluserdistributor,
            'totalorder'         => $totalorder,
            'totalusercustomer'   => $totalusercustomer,
            'todayuserlogin'     => $todayuserlogin,
            'todayusersignup'    => $todayusersignup,
            'todayorder'         => $todayorder,
            'todayunsettleorder' => $todayunsettleorder,
            'product_order_reserve' => $product_order_reserve,
            'product_order_review'           => $product_order_review,
            'paylist'            => $paylist,
            'createlist'         => $createlist,
        ]);
        return $this->view->fetch();
    }

}
